<?=view('header.html')?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">
                <div class="mokui-card-form">
                    <form class="layui-form" id="searchForm">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <div class="layui-form-mid">名称:</div>
                                <div class="layui-input-inline" style="width: 160px;">
                                    <input type="text" name="name" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-form-mid">是否显示:</div>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <select name="hidden">
                                        <option value="0">显示</option>
                                        <option value="1">隐藏</option>
                                    </select>
                                </div>
                                <button class="layui-btn layui-btn-blue" type="button" lay-submit lay-filter="ams_search" id="ams_search">查询</button>
                                <button class="layui-btn layui-btn-primary" type="button" lay-submit lay-filter="ams_reset" id="ams_reset">重置</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <?=view('mains.html')?>
                    <?=view('news/tips.html')?>
                    <table id="listTable" lay-filter="listTable"></table>
                    <script type="text/html" id="toolbarOption">
                    <button type="button" lay-event="cat" class="layui-btn layui-btn-default layui-btn-sm">分类管理</button>
                    <button type="button" lay-event="del" title="您确定要这样操作吗？" class="layui-btn layui-btn-danger layui-btn-sm">删除</button>
                    <button class="layui-btn layui-btn-blue layui-btn-sm" lay-event="add">发布新闻</button>
                </script>
                    <script type="text/html" id="barOption">
                    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                </script>
                </div>
            </div>
        </div>
    </div>
    <?=view('footer.html')?>
    <script type="text/javascript">
        layui.use(['form', 'jquery', 'layer', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                table = layui.table;
            /**index**/

            //数据初始化
            var tableInit = table.render({
                elem: '#listTable',
                toolbar: "#toolbarOption",
                defaultToolbar: [],
                title: '新闻列表',
                url: adminpath + 'news/admin/home/index',
                method: 'post',
                cols: [[
                    { type: 'checkbox' },
                    { field: 'id', title: 'ID', width: 100, unresize: true, sort: true },
                    {
                        field: 'displayorder',
                        width: 80,
                        title: '排序',
                        edit: 'text',
                    },
                    { field: 'catname', title: '分类名称' },
                    {
                        field: 'title',
                        title: '名称/标题'
                    },
                    {
                        field: 'description',
                        title: '描述/简介'
                    },
                    {
                        field: 'status',
                        title: '状态',
                        width: 160,
                        templet: function (d) {
                            if (d.status == '1') {
                                return '<span class="layui-badge layui-bg-blue">正常</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-orange">不显示</span>';
                            }
                        }
                    },
                    {
                        field: 'updatetime_data',
                        title: '更新时间'
                    },
                    {
                        fixed: 'right',
                        title: '操作',
                        toolbar: '#barOption'
                    }
                ]],
                page: {
                    layout: ['prev', 'page', 'next', 'count'],
                    groups: 5,
                    first: true,
                    last: true
                },
                response: {
                    statusCode: 0,
                    countName: 'count'
                },
                parseData: function (res) {
                    return {
                        'code': res.code,
                        'data': res.data.data,
                        'count': res.data.total
                    };
                }
            });

            //表头工具栏
            table.on('toolbar(listTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.id);
                })
                switch (obj.event) {

                    case 'cat':
                        var title = '分类管理';
                        var url = adminpath + 'news/admin/category/index';
                        top.layui.mokui.openTab({ title: title, url: url });
                        return false;
                        break;

                    case 'add':
                        var title = '添加新闻';
                        var url = adminpath + 'news/admin/home/add';
                        top.layui.mokui.openUri(title, url, '80%', '90%');
                        return false;
                        break;

                    case 'del':
                        layer.confirm('确定要删除这些？', function (index) {
                            $.post(adminpath + 'news/admin/home/del', {
                                ids: ids
                            }, function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 2000
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 5,
                                        anim: 6
                                    });
                                }
                                tableInit.reload();
                            }, 'json');
                            layer.close(index);
                        });
                        break;
                }

            });

            //监听表格行操作
            table.on('tool(listTable)', function (obj) {
                var data = obj.data;
                var id = data.id
                switch (obj.event) {
                    case 'edit':
                        var title = '修改新闻(#' + id + ')';
                        var url = adminpath + 'news/admin/home/edit?id=' + id;
                        top.layui.mokui.openUri(title, url, '90%', '90%');
                        break;

                    case 'del':
                        layer.confirm('确定要删除吗？', function (index) {
                            $.post(adminpath + 'news/admin/home/del', {
                                ids: [id]
                            }, function (res) {
                                if (res.code == 0) {
                                    obj.del();
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 2000
                                    }, function () {
                                        // tableInit.reload();
                                    });
                                } else {
                                    layer.msg(res.msg, {
                                        icon: 5,
                                        anim: 6,
                                        time: 2000
                                    }, function () {
                                        // tableInit.reload();
                                    });
                                }
                                tableInit.reload();
                            }, 'json');
                        });
                        break;
                }
            });

            //搜索条件
            form.on('submit(ams_reset)', function (data) {
                $("#searchForm")[0].reset();
                form.render();
                $('#ams_search').trigger('click');
            });

            //标题搜索
            form.on('submit(ams_search)', function (data) {
                table.reload('listTable', {
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });

        })
    </script>
</body>

</html>